package org.googlecode.viewt.core.contant;


public class ConstantManager {
	
	

//	private static boolean initFlag = false;
//
//	private static ConstDataDef constConfig(String name, boolean checkBoth)
//			throws ResourceNotFoundException, ResourceFailException {
//		if ((null == name) || ("".equals(name))) {
//			return null;
//		}
//		final Logger logger = LoggerFactory.getLogger(ConstantManager.class);
//		ConstDataDef def = ConstantDataCfgRepository.getConstConfig(name);
//		if (null != def) {
//			if (logger.isDebugEnabled()) {
//				logger.debug("Alias " + name
//						+ " found from constant data config");
//			}
//			return def;
//		}
//
//		if (!(checkBoth)) {
//			return null;
//		}
//		if (logger.isDebugEnabled()) {
//			logger.debug("Alias " + name + " not found, retry by class name");
//		}
//
//		ConstantDataCfgRepository repository = ConstantDataCfgRepository
//				.getInstance();
//		repository.loadAll();
//		Map constantMap = repository.getConstantMap();
//		Collection defCollection = (null == constantMap) ? null : constantMap
//				.values();
//		if (null != defCollection) {
//			ConstDataDef[] defs = (ConstDataDef[]) defCollection
//					.toArray(new ConstDataDef[0]);
//			int size = (null == defs) ? 0 : defs.length;
//			for (int i = 0; i < size; ++i) {
//				if (!(name.equals(defs[i].getClassName())))
//					continue;
//				if (logger.isDebugEnabled()) {
//					logger.debug("Class name " + name
//							+ " found from constant data config");
//				}
//				return defs[i];
//			}
//
//		}
//
//		if (logger.isDebugEnabled()) {
//			logger.debug("No matched name " + name
//					+ " found by both alias and class name");
//		}
//		return null;
//	}
//
//	 private static void setReloadInterval(String className, int interval)
//			throws ConstantDataFailException {
//		if ((null == className) || ("".equals(className))) {
//			return;
//		}
////		Log logger = LogFactory.getLog(ConstantManager.class);
////		try {
////			BaseUtil.staticMethod(className + ".setReloadInterval",
////					new Integer(interval));
////		} catch (ExpressionErrorException expEx) {
////			logger.error("Set reload interval fail for constant:" + className,
////					expEx);
////			throw new ConstantDataFailException(
////					"Set reload interval fail for constant:" + className, expEx);
////		}
//	}
//
//	private static List queryConstData(ConstDataDef constDef)
//			throws ConstantDataFailException {
//		if ((null == constDef) || (null == constDef.getDbLoaderDef())) {
//			return null;
//		}
//		
//		DbLoaderDef loaderDef = constDef.getDbLoaderDef();
//
//		if (logger.isDebugEnabled()) {
//			logger.debug("Begin retrieve constant data from loader, DAO:"
//					+ loaderDef.getDaoBeanName() + ", query:"
//					+ loaderDef.getNamedQuery() + ", key expression:"
//					+ loaderDef.getKey() + ", value expression:"
//					+ loaderDef.getValue() + ", display expression:"
//					+ loaderDef.getDisplay() + ", input expression:"
//					+ loaderDef.getInput());
//		}
//
//		try {
//			ApplicationContext context = Spring2webContextHelper.getContext();
//			IDAO dao = (IDAO) context.getBean(loaderDef.getDaoBeanName());
//
//			// IAppContext queryCtx = new AppContext(AppContext
//			// .getConfigProperties());
//			// dao.setContext(queryCtx);
//
//			List<IConstant> queryList = dao.listNamedQuery(loaderDef
//					.getNamedQuery());
//
//			if (logger.isDebugEnabled()) {
//				logger.debug("Query for constant data succeed");
//			}
//			
//			List<IConstant> dataList = new ArrayList<IConstant>();
//
//			// IAppContext evalCtx = new AppContext(AppContext
//			// .getConfigProperties());
////			int size = (null == queryList) ? 0 : queryList.size();
////			for (int i = 0; i < size; ++i) {
////				evalCtx.putAttr(loaderDef.getVarName(), queryList.get(i));
////				Object obj = VTLEngineHelper.evalValueExp(evalCtx, loaderDef
////						.getKey());
////				if (null != obj) {
////					String key = (String) TypeUtil.convertType(obj,
////							String.class);
////					obj = VTLEngineHelper.evalValueExp(evalCtx, loaderDef
////							.getValue());
////					String value = (null == obj) ? null : (String) TypeUtil
////							.convertType(obj, String.class);
////
////					String displayValue = null;
////					String inputCode = null;
////					if ((null != loaderDef.getDisplay())
////							&& (!("".equals(loaderDef.getDisplay())))) {
////						obj = VTLEngineHelper.evalValueExp(evalCtx, loaderDef
////								.getDisplay());
////						displayValue = (null == obj) ? null : (String) TypeUtil
////								.convertType(obj, String.class);
////					}
////					if ((null != loaderDef.getInput())
////							&& (!("".equals(loaderDef.getInput())))) {
////						obj = VTLEngineHelper.evalValueExp(evalCtx, loaderDef
////								.getInput());
////						inputCode = (null == obj) ? null : (String) TypeUtil
////								.convertType(obj, String.class);
////					}
////
////					IConstant data = (IConstant) BaseUtil.createBean(constDef
////							.getClassName());
////					data.setKey(key);
////					data.setValue(value);
////					data.setDisplayValue(displayValue);
////					data.setInputCode(inputCode);
////					dataList.add(data);
////
////					if (logger.isDebugEnabled()) {
////						logger.debug("Constant item created:" + data);
////					}
////
////				} else if (logger.isInfoEnabled()) {
////					logger
////							.info("Key of constant item is null, ignore. constant:"
////									+ constDef.getAlias());
////				}
////			}
////
////			if (logger.isDebugEnabled()) {
////				logger.debug("Load constant data:" + constDef.getAlias()
////						+ " from database succeed");
////			}
//			return dataList;
//			
//			
//		} catch (ContextInitException contextEx) {
//			logger.error("Initialize spring context fail", contextEx);
//			throw new ConstantDataFailException(
//					"Initialize spring context fail", contextEx);
//		} catch (BeansException beanEx) {
//			logger.error("Create bean fail", beanEx);
//			throw new ConstantDataFailException("Create bean fail", beanEx);
//		} catch (DAOPerformException daoEx) {
//			logger.error(
//					"Perform database query error, query constant data for "
//							+ constDef.getAlias() + " fail", daoEx);
//			throw new ConstantDataFailException(
//					"Perform database query error, query constant data for "
//							+ constDef.getAlias() + " fail", daoEx);
////		} catch (VTLEvaluateFailException evalEx) {
////			logger.error(
////					"Evaluate constant property error, query constant data for "
////							+ constDef.getAlias() + " fail", evalEx);
////			throw new ConstantDataFailException(
////					"Evaluate constant property error, query constant data for "
////							+ constDef.getAlias() + " fail", evalEx);
////		} catch (TypeConvertException typeEx) {
////			logger.error(
////					"Convert property type error, query constant data for "
////							+ constDef.getAlias() + " fail", typeEx);
////			throw new ConstantDataFailException(
////					"Convert property type error, query constant data for "
////							+ constDef.getAlias() + " fail", typeEx);
////		} catch (ExpressionErrorException expEx) {
////			logger.error(
////					"Expression of constant class error, query constant data for "
////							+ constDef.getAlias() + " fail", expEx);
////			throw new ConstantDataFailException(
////					"Expression of constant class error, query constant data for "
////							+ constDef.getAlias() + " fail", expEx);
//		}
//	}
//
////	private static List checkAndReload(String className, boolean force)
////			throws ConstantDataFailException {
////		if ((null == className) || ("".equals(className))) {
////			return null;
////		}
////		Log logger = LogFactory.getLog(ConstantManager.class);
////		try {
////			boolean reloadFlag = false;
////			if (force) {
////				reloadFlag = true;
////			} else {
////				Boolean reload = (Boolean) BaseUtil.staticMethod(className
////						+ ".needReload");
////				if ((null != reload) && (reload.booleanValue())) {
////					reloadFlag = true;
////				}
////			}
////			if (reloadFlag) {
////				if (logger.isDebugEnabled()) {
////					logger.debug("Reload constant class:" + className);
////				}
////
////				ConstDataDef def = constConfig(className, true);
////				if ((null != def) && (null != def.getDbLoaderDef())) {
////					List constData = queryConstData(def);
////					return ((List) BaseUtil.staticMethod(className
////							+ ".reloadData", constData));
////				}
////
////				return ((List) BaseUtil.staticMethod(className + ".reloadData"));
////			}
////
////		} catch (ExpressionErrorException expEx) {
////			logger.error("Execute reload of " + className
////					+ " error, check and reload fail", expEx);
////			throw new ConstantDataFailException("Execute reload of "
////					+ className + " error, check and reload fail", expEx);
////		} catch (ResourceNotFoundException rnfEx) {
////			logger
////					.error(
////							"Reloadable constant config not found, check and reload fail",
////							rnfEx);
////			throw new ConstantDataFailException(
////					"Reloadable constant config not found, check and reload fail",
////					rnfEx);
////		} catch (ResourceFailException rfEx) {
////			logger
////					.error(
////							"Obtain reloadable constant config error, check and reload fail",
////							rfEx);
////			throw new ConstantDataFailException(
////					"Obtain reloadable constant config error, check and reload fail",
////					rfEx);
////		}
////
////		return null;
////	}
////
//	private static void checkInit() throws ConstantDataFailException {
////		if (initFlag) {
////			return;
////		}
////		Log logger = LogFactory.getLog(ConstantManager.class);
////		try {
////			if (logger.isDebugEnabled()) {
////				logger.debug("Begin initialize constant manager");
////			}
////			String[] names = reloadableConstNames();
////			if (logger.isDebugEnabled()) {
////				logger.debug("Configured constant data count:" + names.length);
////			}
////			int size = (null == names) ? 0 : names.length;
////			for (int i = 0; i < size; ++i) {
////				if (logger.isDebugEnabled())
////					logger.debug("Setting for constant:" + names[i]);
////				ConstDataDef dataDef = constConfig(names[i], false);
////
////				if (dataDef.getReloadInterval() <= 0)
////					continue;
////				if (logger.isDebugEnabled())
////					logger
////							.debug("Set interval and reload constant:"
////									+ names[i]);
////				setReloadInterval(dataDef.getClassName(), dataDef
////						.getReloadInterval());
////				checkAndReload(dataDef.getClassName(), false);
////			}
////
////			initFlag = true;
////
////			if (logger.isDebugEnabled())
////				logger.debug("Constant manager initialized");
////		} catch (ResourceNotFoundException rnfEx) {
////			logger.error("Reloadable constant config not found, init fail",
////					rnfEx);
////			throw new ConstantDataFailException(
////					"Reloadable constant config not found, init fail", rnfEx);
////		} catch (ResourceFailException rfEx) {
////			logger.error("Obtain reloadable constant config error, init fail",
////					rfEx);
////			throw new ConstantDataFailException(
////					"Obtain reloadable constant config error, init fail", rfEx);
////		}
//	}
//
//	public static String[] reloadableConstNames()
//			throws ResourceNotFoundException, ResourceFailException {
//		ConstantDataCfgRepository repository = ConstantDataCfgRepository
//				.getInstance();
//		repository.loadAll();
//		Map constantMap = repository.getConstantMap();
//		Collection names = (null == constantMap) ? null : constantMap.keySet();
//		if (null != names) {
//			return ((String[]) names.toArray(new String[0]));
//		}
//		return new String[0];
//	}
//
////	public static void refreshAll() throws ConstantDataFailException {
////		Log logger = LogFactory.getLog(ConstantManager.class);
////		try {
////			if (logger.isDebugEnabled()) {
////				logger.debug("Begin refresh all constant");
////			}
////			String[] names = reloadableConstNames();
////			int size = (null == names) ? 0 : names.length;
////			for (int i = 0; i < size; ++i) {
////				ConstDataDef dataDef = constConfig(names[i], false);
////
////				setReloadInterval(dataDef.getClassName(), dataDef
////						.getReloadInterval());
////				checkAndReload(dataDef.getClassName(), true);
////			}
////
////			initFlag = true;
////
////			if (logger.isDebugEnabled())
////				logger.debug("Refresh all constant succeed");
////		} catch (ResourceNotFoundException rnfEx) {
////			logger.error("Reloadable constant config not found, refresh fail",
////					rnfEx);
////			throw new ConstantDataFailException(
////					"Reloadable constant config not found, refresh fail", rnfEx);
////		} catch (ResourceFailException rfEx) {
////			logger.error(
////					"Obtain reloadable constant config error, refresh fail",
////					rfEx);
////			throw new ConstantDataFailException(
////					"Obtain reloadable constant config error, refresh fail",
////					rfEx);
////		}
////	}
////
////	public static List refresh(String name) throws ConstantDataFailException {
////		checkInit();
////
////		Log logger = LogFactory.getLog(ConstantManager.class);
////		try {
////			if (logger.isDebugEnabled()) {
////				logger.debug("Begin refresh constant:" + name);
////			}
////			ConstDataDef dataDef = constConfig(name, false);
////			if (null == dataDef) {
////				logger.error("constant:" + name
////						+ " not found in reloadable config, refresh fail");
////				throw new ConstantDataFailException("constant:" + name
////						+ " not found in reloadable config, refresh fail");
////			}
////
////			setReloadInterval(dataDef.getClassName(), dataDef
////					.getReloadInterval());
////			List result = checkAndReload(dataDef.getClassName(), true);
////
////			if (logger.isDebugEnabled())
////				logger.debug("Refresh constant:" + name + " succeed");
////			return result;
////		} catch (ResourceNotFoundException rnfEx) {
////			logger.error("Reloadable constant config not found, refresh fail",
////					rnfEx);
////			throw new ConstantDataFailException(
////					"Reloadable constant config not found, refresh fail", rnfEx);
////		} catch (ResourceFailException rfEx) {
////			logger.error(
////					"Obtain reloadable constant config error, refresh fail",
////					rfEx);
////			throw new ConstantDataFailException(
////					"Obtain reloadable constant config error, refresh fail",
////					rfEx);
////		}
////	}
////
////	private static Object constMethod(String name, String method)
////			throws ConstantDataFailException {
////		if ((null == name) || ("".equals(name))) {
////			return null;
////		}
////		String className = name;
////		Log logger = LogFactory.getLog(ConstantManager.class);
////		try {
////			ConstDataDef dataDef = constConfig(name, false);
////			if (null != dataDef)
////				className = dataDef.getClassName();
////		} catch (ResourceNotFoundException rnfEx) {
////			logger
////					.error(
////							"Reloadable constant config not found, execute method fail",
////							rnfEx);
////			throw new ConstantDataFailException(
////					"Reloadable constant config not found, execute method fail",
////					rnfEx);
////		} catch (ResourceFailException rfEx) {
////			logger
////					.error(
////							"Obtain reloadable constant config error, execute method fail",
////							rfEx);
////			throw new ConstantDataFailException(
////					"Obtain reloadable constant config error, execute method fail",
////					rfEx);
////		}
////
////		try {
////			if (logger.isDebugEnabled()) {
////				logger.debug("Execute method " + method + " of class name:"
////						+ className);
////			}
////			Object result = BaseUtil.staticMethod(className + "." + method);
////			return result;
////		} catch (ExpressionErrorException expEx) {
////			logger.error("Execute '" + method + "' of constant:" + className
////					+ " fail", expEx);
////			throw new ConstantDataFailException("Execute '" + method
////					+ "' of constant:" + className + " fail", expEx);
////		}
////	}
////
////	private static Object constMethod(String name, String method, String param)
////			throws ConstantDataFailException {
////		if ((null == name) || ("".equals(name))) {
////			return null;
////		}
////		String className = name;
////		Log logger = LogFactory.getLog(ConstantManager.class);
////		try {
////			ConstDataDef dataDef = constConfig(name, false);
////			if (null != dataDef)
////				className = dataDef.getClassName();
////		} catch (ResourceNotFoundException rnfEx) {
////			logger
////					.error(
////							"Reloadable constant config not found, execute method fail",
////							rnfEx);
////			throw new ConstantDataFailException(
////					"Reloadable constant config not found, execute method fail",
////					rnfEx);
////		} catch (ResourceFailException rfEx) {
////			logger
////					.error(
////							"Obtain reloadable constant config error, execute method fail",
////							rfEx);
////			throw new ConstantDataFailException(
////					"Obtain reloadable constant config error, execute method fail",
////					rfEx);
////		}
////
////		try {
////			if (logger.isDebugEnabled()) {
////				logger.debug("Execute method " + method + " of class name:"
////						+ className);
////			}
////			Object result = BaseUtil.staticMethod(className + "." + method,
////					param);
////			return result;
////		} catch (ExpressionErrorException expEx) {
////			logger.error("Execute '" + method + "' of constant:" + className
////					+ " fail", expEx);
////			throw new ConstantDataFailException("Execute '" + method
////					+ "' of constant:" + className + " fail", expEx);
////		}
////	}
////
////	private static Object constMethod(Class cls, String method)
////			throws ConstantDataFailException {
////		if (null == cls) {
////			return null;
////		}
////		Log logger = LogFactory.getLog(ConstantManager.class);
////		try {
////			if (logger.isDebugEnabled()) {
////				logger.debug("Execute method " + method + " of class:" + cls);
////			}
////			Object result = BaseUtil.staticMethod(cls.getName() + "." + method);
////			return result;
////		} catch (ExpressionErrorException expEx) {
////			logger.error("Execute '" + method + "' of constant:"
////					+ cls.getName() + " fail", expEx);
////			throw new ConstantDataFailException("Execute '" + method
////					+ "' of constant:" + cls.getName() + " fail", expEx);
////		}
////	}
////
////	private static Object constMethod(Class cls, String method, String param)
////			throws ConstantDataFailException {
////		if (null == cls) {
////			return null;
////		}
////		Log logger = LogFactory.getLog(ConstantManager.class);
////		try {
////			if (logger.isDebugEnabled()) {
////				logger.debug("Execute method " + method + " of class:" + cls);
////			}
////			Object result = BaseUtil.staticMethod(cls.getName() + "." + method,
////					param);
////			return result;
////		} catch (ExpressionErrorException expEx) {
////			logger.error("Execute '" + method + "' of constant:"
////					+ cls.getName() + " fail", expEx);
////			throw new ConstantDataFailException("Execute '" + method
////					+ "' of constant:" + cls.getName() + " fail", expEx);
////		}
////	}
////
////	private static void checkLoadByName(String name)
////			throws ConstantDataFailException {
////		String className = name;
////		Log logger = LogFactory.getLog(ConstantManager.class);
////		try {
////			ConstDataDef dataDef = constConfig(name, false);
////			if (null != dataDef)
////				className = dataDef.getClassName();
////		} catch (ResourceNotFoundException rnfEx) {
////			logger.error(
////					"Reloadable constant config not found, check load fail",
////					rnfEx);
////			throw new ConstantDataFailException(
////					"Reloadable constant config not found, check load fail",
////					rnfEx);
////		} catch (ResourceFailException rfEx) {
////			logger.error(
////					"Obtain reloadable constant config error, check load fail",
////					rfEx);
////			throw new ConstantDataFailException(
////					"Obtain reloadable constant config error, check load fail",
////					rfEx);
////		}
////
////		checkAndReload(className, false);
////	}
////
////	public static Iterator iterator(String name)
////			throws ConstantDataFailException {
////		checkInit();
////		checkLoadByName(name);
////
////		return ((Iterator) constMethod(name, "iterator"));
////	}
////
////	public static Iterator iterator(Class cls) throws ConstantDataFailException {
////		checkInit();
////		checkAndReload(cls.getName(), false);
////		return ((Iterator) constMethod(cls, "iterator"));
////	}
////
////	public static List items(String name) throws ConstantDataFailException {
////		checkInit();
////		checkLoadByName(name);
////		return ((List) constMethod(name, "items"));
////	}
////
////	public static List items(Class cls) throws ConstantDataFailException {
////		checkInit();
////		checkAndReload(cls.getName(), false);
////		return ((List) constMethod(cls, "items"));
////	}
////
////	public static IConstant item(String name) throws ConstantDataFailException {
////		Log logger = LogFactory.getLog(ConstantManager.class);
////		if ((null == name) || ("".equals(name))) {
////			return null;
////		}
////		int index = name.lastIndexOf(".");
////		if (index <= 0) {
////			logger.error("Item name:" + name
////					+ " is invalid, can not get constant item");
////			throw new ConstantDataFailException("Item name:" + name
////					+ " is invalid, can not get constant item");
////		}
////		String namePart = name.substring(0, index);
////		String keyPart = name.substring(index + 1);
////		return item(namePart, keyPart);
////	}
////
////	public static IConstant item(String name, String key)
////			throws ConstantDataFailException {
////		checkInit();
////		checkLoadByName(name);
////		return ((IConstant) constMethod(name, "item", key));
////	}
////
////	public static IConstant item(Class cls, String key)
////			throws ConstantDataFailException {
////		checkInit();
////		checkAndReload(cls.getName(), false);
////		return ((IConstant) constMethod(cls, "item", key));
////	}
////
////	public static IConstant itemByValue(String name, String value)
////			throws ConstantDataFailException {
////		checkInit();
////		checkLoadByName(name);
////		return ((IConstant) constMethod(name, "itemByValue", value));
////	}
////
////	public static IConstant itemByValue(Class cls, String value)
////			throws ConstantDataFailException {
////		checkInit();
////		checkAndReload(cls.getName(), false);
////		return ((IConstant) constMethod(cls, "itemByValue", value));
////	}
////
////	public static IConstant itemByInput(String name, String inputCode)
////			throws ConstantDataFailException {
////		checkInit();
////		checkLoadByName(name);
////		return ((IConstant) constMethod(name, "itemByInput", inputCode));
////	}
////
////	public static IConstant itemByInput(Class cls, String inputCode)
////			throws ConstantDataFailException {
////		checkInit();
////		checkAndReload(cls.getName(), false);
////		return ((IConstant) constMethod(cls, "itemByInput", inputCode));
////	}
////
////	public static List itemsByInput(String name, String beginInput)
////			throws ConstantDataFailException {
////		checkInit();
////		checkLoadByName(name);
////		return ((List) constMethod(name, "itemsByInput", beginInput));
////	}
////
////	public static List itemsByInput(Class cls, String beginInput)
////			throws ConstantDataFailException {
////		checkInit();
////		checkAndReload(cls.getName(), false);
////		return ((List) constMethod(cls, "itemsByInput", beginInput));
////	}
}